home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / tar.gnu / tar-dist / list.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-11  |  15.9 KB  |  707 lines

  1. /* List a tar archive.
  2.    Copyright (C) 1988 Free Software Foundation
  3.  
  4. This file is part of GNU Tar.
  5.  
  6. GNU Tar is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Tar is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Tar; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*
  21.  * List a tar archive.
  22.  *
  23.  * Also includes support routines for reading a tar archive.
  24.  *
  25.  * this version written 26 Aug 1985 by John Gilmore (ihnp4!hoptoad!gnu).
  26.  *
  27.  * @(#)list.c 1.31 11/5/87 - gnu
  28.  */
  29. #include <stdio.h>
  30. #include <ctype.h>
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #ifndef    MSDOS
  34. #include <sys/file.h>
  35. #endif    /* MSDOS */
  36.  
  37. #ifdef USG
  38. #include <sys/sysmacros.h>    /* major() and minor() defined here */
  39. #endif
  40.  
  41. char *ctime();                /* From libc.a */
  42.  
  43. #define    isodigit(c)    ( ((c) >= '0') && ((c) <= '7') )
  44.  
  45. #include "tar.h"
  46. #include "port.h"
  47.  
  48. extern FILE *msg_file;
  49.  
  50. long from_oct();            /* Decode octal number */
  51. void demode();                /* Print file mode */
  52.  
  53. union record *head;            /* Points to current archive header */
  54. struct stat hstat;            /* Stat struct corresponding */
  55. int head_standard;            /* Tape header is in ANSI format */
  56.  
  57. void print_header();
  58. void skip_file();
  59. void skip_extended_headers();
  60.  
  61. extern char *quote_copy_string();
  62.  
  63.  
  64. /*
  65.  * Main loop for reading an archive.
  66.  */
  67. void
  68. read_and(do_something)
  69.     void (*do_something)();
  70. {
  71.     int status = 3;            /* Initial status at start of archive */
  72.     int prev_status;
  73.     extern time_t new_time;
  74.     char save_linkflag;
  75.     
  76.     name_gather();            /* Gather all the names */
  77.     open_archive(1);        /* Open for reading */
  78.  
  79.     for(;;) {
  80.         prev_status = status;
  81.         status = read_header();
  82.         switch (status) {
  83.  
  84.         case 1:            /* Valid header */
  85.             /* We should decode next field (mode) first... */
  86.             /* Ensure incoming names are null terminated. */
  87.             head->header.name[NAMSIZ-1] = '\0';
  88.             
  89.             if (   !name_match(head->header.name)
  90.                  || (f_new_files && hstat.st_mtime<new_time)
  91.                  || (f_exclude && check_exclude(head->header.name))) {
  92.  
  93.                 int isextended = 0;
  94.                 
  95.                 /* Skip past it in the archive */
  96.                 if (head->header.isextended)
  97.                     isextended = 1;
  98.                 save_linkflag = head->header.linkflag;
  99.                 userec(head);
  100.                 if (isextended) {
  101. /*                    register union record *exhdr;
  102.  
  103.                     for (;;) {
  104.                         exhdr = findrec();
  105.                         if (!exhdr->ext_hdr.isextended) {
  106.                             userec(exhdr);
  107.                             break;
  108.                         }
  109.                     }
  110.                     userec(exhdr);*/
  111.                     skip_extended_headers();
  112.                 }
  113.                 /* Skip to the next header on the archive */
  114.                 if(save_linkflag != LF_DIR)
  115.                     skip_file((long)hstat.st_size);
  116.                 continue;
  117.  
  118.             }
  119.  
  120.             (*do_something)();
  121.             continue;
  122.  
  123.             /*
  124.              * If the previous header was good, tell them
  125.              * that we are skipping bad ones.
  126.              */
  127.         case 0:            /* Invalid header */
  128.             userec(head);
  129.             switch (prev_status) {
  130.             case 3:        /* Error on first record */
  131.                 msg("Hmm, this doesn't look like a tar archive.");
  132.                 /* FALL THRU */
  133.             case 2:        /* Error after record of zeroes */
  134.             case 1:        /* Error after header rec */
  135.                 msg("Skipping to next file header...\n");
  136.             case 0:        /* Error after error */
  137.                 break;
  138.             }
  139.             continue;
  140.  
  141.         case 2:            /* Record of zeroes */
  142.             userec(head);
  143.             status = prev_status;    /* If error after 0's */
  144.             if (f_ignorez)    
  145.                 continue;
  146.             /* FALL THRU */
  147.         case EOF:        /* End of archive */
  148.             break;
  149.         }
  150.         break;
  151.     };
  152.  
  153.     close_archive();
  154.     names_notfound();        /* Print names not found */
  155. }        
  156.  
  157.  
  158. /*
  159.  * Print a header record, based on tar options.
  160.  */
  161. void
  162. list_archive()
  163. {
  164.     extern char *save_name;
  165.     int    isextended = 0; /* Flag to remember if head is extended */
  166.     
  167.     /* Save the record */
  168.     saverec(&head);
  169.  
  170.     /* Print the header record */
  171.     if (f_verbose) {
  172.         if (f_verbose > 1)
  173.             decode_header(head, &hstat, &head_standard, 0);
  174.         print_header();
  175.     }
  176.  
  177.     if(f_gnudump && head->header.linkflag==LF_DUMPDIR) {
  178.         size_t    size, written, check;
  179.         char    *data;
  180.         extern int errno;
  181.         extern long save_totsize;
  182.         extern long save_sizeleft;
  183.  
  184.         userec(head);
  185.         if(f_multivol) {
  186.             save_name = head->header.name;
  187.             save_totsize=hstat.st_size;
  188.         }
  189.         for(size = hstat.st_size;size>0;size-=written) {
  190.             if(f_multivol)
  191.                 save_sizeleft=size;
  192.             data = findrec()->charptr;
  193.             if(data==NULL) {
  194.                 msg("EOF in archive file?");
  195.                 break;
  196.             }
  197.             written = endofrecs()->charptr - data;
  198.             if(written>size)
  199.                 written=size;
  200.             errno=0;
  201.             check=fwrite(data,sizeof(char), written, msg_file);
  202.             userec((union record *)(data+written - 1));
  203.             if(check!=written) {
  204.                 msg_perror("only wrote %ld of %ld bytes to file %s",check, written,head->header.name);
  205.                 skip_file((long)(size)-written);
  206.                 break;
  207.             }
  208.         }
  209.         if(f_multivol)
  210.             save_name = 0;
  211.         saverec((union record **) 0);    /* Unsave it */
  212.         fputc('\n',msg_file);
  213.         fflush(msg_file);
  214.         return;
  215.  
  216.     }
  217.     saverec((union record **) 0);    /* Unsave it */
  218.     /* Check to see if we have an extended header to skip over also */
  219.     if (head->header.isextended) 
  220.         isextended = 1;
  221.         
  222.     /* Skip past the header in the archive */
  223.     userec(head);
  224.  
  225.     /*
  226.       * If we needed to skip any extended headers, do so now, by
  227.       * reading extended headers and skipping past them in the 
  228.      * archive.
  229.      */
  230.     if (isextended) {
  231. /*        register union record *exhdr;
  232.  
  233.         for (;;) {
  234.             exhdr = findrec();
  235.  
  236.             if (!exhdr->ext_hdr.isextended) {
  237.                 userec(exhdr);
  238.                 break;
  239.             }
  240.             userec(exhdr);
  241.         }*/
  242.         skip_extended_headers();
  243.     }
  244.             
  245.     if(f_multivol)
  246.         save_name=head->header.name;
  247.     /* Skip to the next header on the archive */
  248.         
  249.     skip_file((long) hstat.st_size);
  250.         
  251.     if(f_multivol)
  252.         save_name = 0;
  253. }
  254.  
  255.  
  256. /*
  257.  * Read a record that's supposed to be a header record.
  258.  * Return its address in "head", and if it is good, the file's
  259.  * size in hstat.st_size.
  260.  *
  261.  * Return 1 for success, 0 if the checksum is bad, EOF on eof,
  262.  * 2 for a record full of zeros (EOF marker).
  263.  *
  264.  * You must always userec(head) to skip past the header which this
  265.  * routine reads.
  266.  */
  267. int
  268. read_header()
  269. {
  270.     register int    i;
  271.     register long    sum, recsum;
  272.     register char    *p;
  273.     register union record *header;
  274.     long    from_oct();
  275.  
  276.     header = findrec();
  277.     head = header;        /* This is our current header */
  278.     if (NULL == header) return EOF;
  279.  
  280.     recsum = from_oct(8,  header->header.chksum);
  281.  
  282.     sum = 0;
  283.     p = header->charptr;
  284.     for (i = sizeof(*header); --i >= 0;) {
  285.         /*
  286.          * We can't use unsigned char here because of old compilers,
  287.          * e.g. V7.
  288.          */
  289.         sum += 0xFF & *p++;
  290.     }
  291.  
  292.     /* Adjust checksum to count the "chksum" field as blanks. */
  293.     for (i = sizeof(header->header.chksum); --i >= 0;)
  294.         sum -= 0xFF & header->header.chksum[i];
  295.     sum += ' '* sizeof header->header.chksum;    
  296.  
  297.     if (sum == recsum) {
  298.         /*
  299.          * Good record.  Decode file size and return.
  300.          */
  301.         if (header->header.linkflag == LF_LINK)
  302.             hstat.st_size = 0;    /* Links 0 size on tape */
  303.         else
  304.             hstat.st_size = from_oct(1+12, header->header.size);
  305.         return 1;
  306.     }
  307.  
  308.     if (sum == 8*' ') {
  309.         /*
  310.          * This is a zeroed record...whole record is 0's except
  311.          * for the 8 blanks we faked for the checksum field.
  312.          */
  313.         return 2;
  314.     }
  315.  
  316.     return 0;
  317. }
  318.  
  319.  
  320. /* 
  321.  * Decode things from a file header record into a "struct stat".
  322.  * Also set "*stdp" to !=0 or ==0 depending whether header record is "Unix
  323.  * Standard" tar format or regular old tar format.
  324.  *
  325.  * read_header() has already decoded the checksum and length, so we don't.
  326.  *
  327.  * If wantug != 0, we want the uid/group info decoded from Unix Standard
  328.  * tapes (for extraction).  If == 0, we are just printing anyway, so save time.
  329.  *
  330.  * decode_header should NOT be called twice for the same record, since the
  331.  * two calls might use different "wantug" values and thus might end up with
  332.  * different uid/gid for the two calls.  If anybody wants the uid/gid they
  333.  * should decode it first, and other callers should decode it without uid/gid
  334.  * before calling a routine, e.g. print_header, that assumes decoded data.
  335.  */
  336. decode_header(header, st, stdp, wantug)
  337.     register union record    *header;
  338.     register struct stat    *st;
  339.     int    *stdp;
  340.     int    wantug;
  341. {
  342.  
  343.     long from_oct();
  344.  
  345.     st->st_mode = from_oct(8,  header->header.mode);
  346.     st->st_mtime = from_oct(1+12, header->header.mtime);
  347.     if(f_gnudump) {
  348.         st->st_atime = from_oct(1+12, header->header.atime);
  349.         st->st_ctime = from_oct(1+12, header->header.ctime);
  350.     }
  351.     
  352.     if (0==strcmp(header->header.magic, TMAGIC)) {
  353.         /* Unix Standard tar archive */
  354.         *stdp = 1;
  355.         if (wantug) {
  356. #ifdef NONAMES
  357.             st->st_uid = from_oct(8,  header->header.uid);
  358.             st->st_gid = from_oct(8,  header->header.gid);
  359. #else
  360.             st->st_uid = finduid(header->header.uname);
  361.             st->st_gid = findgid(header->header.gname);
  362. #endif
  363.         }
  364.         switch (header->header.linkflag) {
  365.         case LF_BLK: case LF_CHR:
  366.             st->st_rdev = makedev(from_oct(8, header->header.devmajor),
  367.                        from_oct(8, header->header.devminor));
  368.         }
  369.     } else {
  370.         /* Old fashioned tar archive */
  371.         *stdp = 0;
  372.         st->st_uid = from_oct(8,  header->header.uid);
  373.         st->st_gid = from_oct(8,  header->header.gid);
  374.         st->st_rdev = 0;
  375.     }
  376. }
  377.  
  378.  
  379. /*
  380.  * Quick and dirty octal conversion.
  381.  *
  382.  * Result is -1 if the field is invalid (all blank, or nonoctal).
  383.  */
  384. long
  385. from_oct(digs, where)
  386.     register int    digs;
  387.     register char    *where;
  388. {
  389.     register long    value;
  390.  
  391.     while (isspace(*where)) {        /* Skip spaces */
  392.         where++;
  393.         if (--digs <= 0)
  394.             return -1;        /* All blank field */
  395.     }
  396.     value = 0;
  397.     while (digs > 0 && isodigit(*where)) {    /* Scan til nonoctal */
  398.         value = (value << 3) | (*where++ - '0');
  399.         --digs;
  400.     }
  401.  
  402.     if (digs > 0 && *where && !isspace(*where))
  403.         return -1;            /* Ended on non-space/nul */
  404.  
  405.     return value;
  406. }
  407.  
  408.  
  409. /*
  410.  * Actually print it.
  411.  *
  412.  * Plain and fancy file header block logging.
  413.  * Non-verbose just prints the name, e.g. for "tar t" or "tar x".
  414.  * This should just contain file names, so it can be fed back into tar
  415.  * with xargs or the "-T" option.  The verbose option can give a bunch
  416.  * of info, one line per file.  I doubt anybody tries to parse its
  417.  * format, or if they do, they shouldn't.  Unix tar is pretty random here
  418.  * anyway.
  419.  *
  420.  * Note that print_header uses the globals <head>, <hstat>, and
  421.  * <head_standard>, which must be set up in advance.  This is not very clean
  422.  * and should be cleaned up.  FIXME.
  423.  */
  424. #define    UGSWIDTH    11        /* min width of User, group, size */
  425. #define    DATEWIDTH    19        /* Last mod date */
  426. static int    ugswidth = UGSWIDTH;    /* Max width encountered so far */
  427.  
  428. void
  429. print_header()
  430. {
  431.     char modes[11];
  432.     char *timestamp;
  433.     char uform[11], gform[11];    /* These hold formatted ints */
  434.     char *user, *group;
  435.     char size[24];        /* Holds a formatted long or maj, min */
  436.     long longie;        /* To make ctime() call portable */
  437.     int    pad;
  438.     char *name;
  439.     extern long baserec;
  440.  
  441.     if(f_sayblock)
  442.         fprintf(msg_file,"rec %10d: ",baserec + (ar_record - ar_block));
  443.     /* annofile(msg_file, (char *)NULL); */
  444.  
  445.     if (f_verbose <= 1) {
  446.         /* Just the fax, mam. */
  447.         char *name;
  448.  
  449.         name=quote_copy_string(head->header.name);
  450.         if(name==0)
  451.             name=head->header.name;
  452.         fprintf(msg_file, "%s\n", name);
  453.         if(name!=head->header.name)
  454.             free(name);
  455.     } else {
  456.         /* File type and modes */
  457.         modes[0] = '?';
  458.         switch (head->header.linkflag) {
  459.         case LF_VOLHDR:
  460.             modes[0]='V';
  461.             break;
  462.  
  463.         case LF_MULTIVOL:
  464.             modes[0]='M';
  465.             break;
  466.  
  467.         case LF_SPARSE:
  468.         case LF_NORMAL:
  469.         case LF_OLDNORMAL:
  470.         case LF_LINK:
  471.                 modes[0] = '-'; 
  472.                 if ('/' == head->header.name[strlen(head->header.name)-1])
  473.                     modes[0] = 'd';
  474.                 break;
  475.         case LF_DUMPDIR:modes[0] = 'd'; break;
  476.         case LF_DIR:    modes[0] = 'd'; break;
  477.         case LF_SYMLINK:modes[0] = 'l'; break;
  478.         case LF_BLK:    modes[0] = 'b'; break;
  479.         case LF_CHR:    modes[0] = 'c'; break;
  480.         case LF_FIFO:    modes[0] = 'p'; break;    
  481.         case LF_CONTIG:    modes[0] = 'C'; break;
  482.         }
  483.  
  484.         demode((unsigned)hstat.st_mode, modes+1);
  485.  
  486.         /* Timestamp */
  487.         longie = hstat.st_mtime;
  488.         timestamp = ctime(&longie);
  489.         timestamp[16] = '\0';
  490.         timestamp[24] = '\0';
  491.  
  492.         /* User and group names */
  493.         if (*head->header.uname && head_standard) {
  494.             user  = head->header.uname;
  495.         } else {
  496.             user = uform;
  497.             (void)sprintf(uform, "%d", (int)hstat.st_uid);
  498.         }
  499.         if (*head->header.gname && head_standard) {
  500.             group = head->header.gname;
  501.         } else {
  502.             group = gform;
  503.             (void)sprintf(gform, "%d", (int)hstat.st_gid);
  504.         }
  505.  
  506.         /* Format the file size or major/minor device numbers */
  507.         switch (head->header.linkflag) {
  508.         case LF_CHR:
  509.         case LF_BLK:
  510.             (void)sprintf(size, "%d,%d",
  511.                     major(hstat.st_rdev),
  512.                     minor(hstat.st_rdev));
  513.             break;
  514.         case LF_SPARSE:
  515.             (void)sprintf(size, "%ld",
  516.                  from_oct(1+12, head->header.realsize));
  517.             break;
  518.         default:
  519.             (void)sprintf(size, "%ld", (long)hstat.st_size);
  520.         }
  521.  
  522.         /* Figure out padding and print the whole line. */
  523.         pad = strlen(user) + strlen(group) + strlen(size) + 1;
  524.         if (pad > ugswidth) ugswidth = pad;
  525.  
  526.         name = quote_copy_string(head->header.name);
  527.         if(!name)
  528.             name=head->header.name;
  529.         fprintf(msg_file, "%s %s/%s %*s%s %s %s %.*s",
  530.             modes,
  531.             user,
  532.             group,
  533.             ugswidth - pad,
  534.             "",
  535.             size,
  536.             timestamp+4, timestamp+20,
  537.             sizeof(head->header.name),
  538.             name);
  539.  
  540.         if(name!=head->header.name)
  541.             free(name);
  542.         switch (head->header.linkflag) {
  543.         case LF_SYMLINK:
  544.             name=quote_copy_string(head->header.linkname);
  545.             if(!name)
  546.                 name=head->header.linkname;
  547.             fprintf(msg_file, " -> %s\n", name);
  548.             if(name!=head->header.linkname)
  549.                 free(name);
  550.             break;
  551.  
  552.         case LF_LINK:
  553.             name=quote_copy_string(head->header.linkname);
  554.             if(!name)
  555.                 name=head->header.linkname;
  556.             fprintf(msg_file, " link to %s\n", head->header.linkname);
  557.             if(name!=head->header.linkname)
  558.                 free(name);
  559.             break;
  560.  
  561.         default:
  562.             fprintf(msg_file, " unknown file type '%c'\n",
  563.                 head->header.linkflag);
  564.             break;
  565.  
  566.         case LF_OLDNORMAL:
  567.         case LF_NORMAL:
  568.         case LF_SPARSE:
  569.         case LF_CHR:
  570.         case LF_BLK:
  571.         case LF_DIR:
  572.         case LF_FIFO:
  573.         case LF_CONTIG:
  574.         case LF_DUMPDIR:
  575.             putc('\n', msg_file);
  576.             break;
  577.  
  578.         case LF_VOLHDR:
  579.             fprintf(msg_file, "--Volume Header--\n");
  580.             break;
  581.             
  582.         case LF_MULTIVOL:
  583.             fprintf(msg_file, "--Continued at byte %ld--\n",from_oct(1+12,head->header.offset));
  584.             break;
  585.         }
  586.     }
  587.     fflush(msg_file);
  588. }
  589.  
  590. /*
  591.  * Print a similar line when we make a directory automatically.
  592.  */
  593. void
  594. pr_mkdir(pathname, length, mode)
  595.     char *pathname;
  596.     int length;
  597.     int mode;
  598. {
  599.     char modes[11];
  600.     char *name;
  601.     extern long baserec;
  602.  
  603.     if (f_verbose > 1) {
  604.         /* File type and modes */
  605.         modes[0] = 'd';
  606.         demode((unsigned)mode, modes+1);
  607.  
  608.         if(f_sayblock)
  609.             fprintf(msg_file,"rec %10d: ",baserec + (ar_record - ar_block));
  610.         /* annofile(msg_file, (char *)NULL); */
  611.         name=quote_copy_string(pathname);
  612.         if(!name)
  613.             name=pathname;
  614.         fprintf(msg_file, "%s %*s %.*s\n",
  615.             modes,
  616.             ugswidth+DATEWIDTH,
  617.             "Creating directory:",
  618.             length,
  619.             pathname);
  620.         if(name!=pathname)
  621.             free(name);
  622.     }
  623. }
  624.  
  625.  
  626. /*
  627.  * Skip over <size> bytes of data in records in the archive.
  628.  */
  629. void
  630. skip_file(size)
  631.     register long size;
  632. {
  633.     union record *x;
  634.     extern long save_totsize;
  635.     extern long save_sizeleft;
  636.  
  637.     if(f_multivol) {
  638.         save_totsize=size;
  639.         save_sizeleft=size;
  640.     }
  641.  
  642.     while (size > 0) {
  643.         x = findrec();
  644.         if (x == NULL) {    /* Check it... */
  645.             msg("Unexpected EOF on archive file");
  646.             exit(EX_BADARCH);
  647.         }
  648.         userec(x);
  649.         size -= RECORDSIZE;
  650.         if(f_multivol)
  651.             save_sizeleft-=RECORDSIZE;
  652.     }
  653. }
  654.  
  655. void
  656. skip_extended_headers()
  657. {
  658.     register union record *exhdr;
  659.  
  660.     for (;;) {
  661.         exhdr = findrec();
  662.         if (!exhdr->ext_hdr.isextended) {
  663.             userec(exhdr);
  664.             break;
  665.         }
  666.     }
  667.     userec(exhdr);
  668. }
  669.  
  670. /*
  671.  * Decode the mode string from a stat entry into a 9-char string and a null.
  672.  */
  673. void
  674. demode(mode, string)
  675.     register unsigned mode;
  676.     register char *string;
  677. {
  678.     register unsigned mask;
  679.     register char *rwx = "rwxrwxrwx";
  680.  
  681.     for (mask = 0400; mask != 0; mask >>= 1) {
  682.         if (mode & mask)
  683.             *string++ = *rwx++;
  684.         else {
  685.             *string++ = '-';
  686.             rwx++;
  687.         }
  688.     }
  689.  
  690.     if (mode & S_ISUID)
  691.         if (string[-7] == 'x')
  692.             string[-7] = 's';
  693.         else
  694.             string[-7] = 'S';
  695.     if (mode & S_ISGID)
  696.         if (string[-4] == 'x')
  697.             string[-4] = 's';
  698.         else
  699.             string[-4] = 'S';
  700.     if (mode & S_ISVTX)
  701.         if (string[-1] == 'x')
  702.             string[-1] = 't';
  703.         else
  704.             string[-1] = 'T';
  705.     *string = '\0';
  706. }
  707.